package cn.tj212.xiaoyin.portal.controller;

import cn.tj212.xiaoyin.common.page.TailPage;
import cn.tj212.xiaoyin.common.storage.QiniuStorage;
import cn.tj212.xiaoyin.common.web.JsonView;
import cn.tj212.xiaoyin.common.web.SessionContext;
import cn.tj212.xiaoyin.core.course.domain.CourseComment;
import cn.tj212.xiaoyin.core.course.domain.CourseSection;
import cn.tj212.xiaoyin.core.course.service.ICourseCommentService;
import cn.tj212.xiaoyin.core.course.service.ICourseSectionService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@Controller
@RequestMapping("/courseComment")
public class CourseCommentController {

    @Autowired
    private ICourseCommentService courseCommentService;

    @Autowired
    private ICourseSectionService courseSectionService;

    /**
     * 加载评论&答疑
     * type:课程类型
     *
     * @param queryEntity
     * @param page
     * @return
     */
    @RequestMapping(value = "/segment")
    public ModelAndView segment(CourseComment queryEntity, TailPage<CourseComment> page) {
        if (null == queryEntity.getCourseId() || null == queryEntity.getType())
            return new ModelAndView("error/404");

        ModelAndView mv = new ModelAndView("commentSegment");
        TailPage<CourseComment> commentPage = courseCommentService.queryPage(queryEntity, page);

        for (CourseComment item : commentPage.getItems()
                ) {
            if (StringUtils.isNotEmpty(item.getHeader())) {
                item.setHeader(QiniuStorage.getUrl(item.getHeader()));
            }
        }
        mv.addObject("page", commentPage);
        return mv;
    }

    /**
     * 发表评论
     * @param request
     * @param entity
     * @param identifyCode
     * @return
     */
    @RequestMapping("/doComment")
    @ResponseBody
    public String doComment(HttpServletRequest request, CourseComment entity, String identifyCode) {
        //验证码判断
        if (null == identifyCode || (identifyCode != null && identifyCode.equalsIgnoreCase(SessionContext.getIdentifyCode(request)))) {
            return new JsonView(2).toString();//验证码错误
        }

        //文字太长
        if (entity.getContent().trim().length() > 200 || entity.getContent().length() == 0) {
            return new JsonView(3).toString();//文字太长或者为空
        }

        if (null != entity.getRefId()) {//来自于个人中心的评论
            CourseComment refComment = courseCommentService.getById(entity.getRefId());
            if (null != refComment) {
                CourseSection courseSection = courseSectionService.getById(refComment.getSectionId());
                if (null != courseSection) {
                    entity.setRefContent(refComment.getRefContent());
                    entity.setRefId(entity.getRefId());
                    entity.setCourseId(refComment.getCourseId());
                    entity.setSectionId(refComment.getSectionId());
                    entity.setSectionTitle(courseSection.getName());

                    entity.setToUsername(refComment.getUsername());//引用的评论的username
                    entity.setUsername(SessionContext.getUsername());
                    entity.setCreateTime(new Date());
                    entity.setCreateUser(SessionContext.getUsername());
                    entity.setUpdateTime(new Date());
                    entity.setUpdateUser(SessionContext.getUsername());

                    courseCommentService.createSelectivity(entity);
                    return new JsonView(0).toString();
                }
            }
        }else {
            CourseSection courseSection=courseSectionService.getById(entity.getSectionId());
            if (null!=courseSection){
                entity.setSectionTitle(courseSection.getName());
                entity.setToUsername(entity.getCreateUser());
                entity.setUsername(SessionContext.getUsername());
                entity.setCreateTime(new Date());
                entity.setCreateUser(SessionContext.getUsername());
                entity.setUpdateTime(new Date());
                entity.setUpdateUser(SessionContext.getUsername());

                courseCommentService.createSelectivity(entity);
                return new JsonView(0).toString();
            }
        }
        return new JsonView(1).toString();
    }

}
